package com.example.manu_facturer_orders.service.impl;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.manu_facturer_orders.mapper.ManuFacturerMapper;
import com.example.manu_facturer_orders.pojo.ManuFacturerVO;
import com.example.manu_facturer_orders.service.ManuFacturerService;
import commons.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: tobed
 * @date: 2022/9/8
 * @time: 8:18
 */
@Service
public class ManuFacturerServiceImpl implements ManuFacturerService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    ManuFacturerMapper manuFacturerMapper;

    private static final String  key = "L_Factur";
    private static final String  listNoPage_factur = "ListNoPage_Factur";

    private void flushRedis(String key){stringRedisTemplate.delete(key);}


    /**
     * 厂商全查
     * @return
     */
    @Override
    public Page<ManuFacturerVO> listManuFacturer(String manufacturerCode, String manufacturerType, String manufacturerUseType, Page<ManuFacturerVO> page) {
        return manuFacturerMapper.listManuFacturer(manufacturerCode,manufacturerType,manufacturerUseType,page);
    }


    /**
     * 自行注册厂商全查
     * @return
     */
    @Override
    public Page<ManuFacturerVO> listNoManuFacturer(String manufacturerCode, String manufacturerType, String manufacturerUseType, Page<ManuFacturerVO> page) {
        return manuFacturerMapper.listNoManuFacturer(manufacturerCode,manufacturerType,manufacturerUseType,page);
    }

    /**
     * 自行注册厂商全查无分页
     * @return
     */
    @Override
    public List<ManuFacturerVO> listNo() {
        if (StrUtil.isBlank(stringRedisTemplate.opsForValue().get(listNoPage_factur))){
            List<ManuFacturerVO> manuFacturerVOS = manuFacturerMapper.listNo();
            stringRedisTemplate.opsForValue().set(listNoPage_factur,JSONUtil.toJsonStr(manuFacturerVOS));
            return manuFacturerVOS;
        }else{
            String jsonStr = stringRedisTemplate.opsForValue().get(listNoPage_factur);
            List<ManuFacturerVO> manuFacturerVOS = JSONUtil.toBean(jsonStr, new TypeReference<List<ManuFacturerVO>>() {
            }, true);
            return manuFacturerVOS;
        }
    }


    /**
     * 真全查
     * @return
     */
    @Override
    public List<ManuFacturerVO> allList() {
        return manuFacturerMapper.allList();
    }

    /**
     * 停用/启用
     * @param id
     * @param manufacturerUseType
     * @return
     */
    @Override
    public Result closeManuFacturer(Integer id, Integer manufacturerUseType) {
        int type = 0;
        if (manufacturerUseType == 0){
            type = 1;
        }
        int i = manuFacturerMapper.closeManuFacturer(id,type);
        if (i != 0){
            flushRedis(key + id);
            return Result.success();
        }else{
            return Result.error();
        }
    }

    /**
     * 签约厂商
     * @param contractStartDate
     * @param contractEndDate
     * @return
     */
    @Override
    public Result contractManuFacturer(String contractStartDate, String contractEndDate,Integer id) {
        int i = manuFacturerMapper.contractManuFacturer(contractStartDate,contractEndDate,id);
        if (i!=0){
            stringRedisTemplate.delete(listNoPage_factur);
            return Result.success();
        }else{
            return Result.error();
        }
    }

    /**
     * 自行注册厂商处理
     * @param id
     * @param manufacturerDelMessage
     * @return
     */
    @Override
    public Result dealManuFacturer(Integer id, String manufacturerDelMessage,Integer manufacturerDelType) {
        int i = manuFacturerMapper.dealManuFacturer(id,manufacturerDelMessage,manufacturerDelType);
        if (i!=0){
            return Result.success();
        }else{
            return Result.error();
        }
    }

    /**
     * 单查名称
     * @param id
     * @return
     */
    @Override
    public String getNameByPkid(Integer id) {
        ManuFacturerVO manuFacturer = manuFacturerMapper.getNameByPkid(id);
        return manuFacturer.getManufacturerName();
    }

    /**
     * 解约厂商
     * @param id
     * @return
     */
    @Override
    public Result jieyueFactur(Integer id) {
        int i = manuFacturerMapper.jieyueFactur(id);
        if (i!=0){
            return Result.success();
        }else{
            return Result.error();
        }
    }
}
